package com.github.phoenix.widget;

import com.github.phoenix.ResourceTable;

import ohos.agp.components.*;
import ohos.app.Context;

/**
 * 自定义防支付宝, 京东密码输入框
 *
 * @author Phoenix
 * @date 2016-10-9 11:01
 */
public class PayEditText extends DirectionalLayout {
    private Context context;
    private Text tvFirst, tvSecond, tvThird, tvForth, tvFifth, tvSixth;
    private StringBuilder mPassword;
    private OnInputFinishedListener onInputFinishedListener;

    private static final String STANCE_STRING = "•";

    /**
     * @param context context
     */
    public PayEditText(Context context) {
        this(context, null);
    }

    /**
     * @param context context
     * @param attrs   attrs
     */
    public PayEditText(Context context, AttrSet attrs) {
        this(context, attrs, "");
    }

    /**
     * @param context      context
     * @param attrs        attrs
     * @param defStyleAttr defStyleAttr
     */
    public PayEditText(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initPayEditText();
    }

    /**
     * 初始化PayEditText
     */
    private void initPayEditText() {
        Component component = LayoutScatter.getInstance(context).parse(
                ResourceTable.Layout_view_pay_edit, null, false);
        tvFirst = (Text) component.findComponentById(ResourceTable.Id_tv_pay1);
        tvSecond = (Text) component.findComponentById(ResourceTable.Id_tv_pay2);
        tvThird = (Text) component.findComponentById(ResourceTable.Id_tv_pay3);
        tvForth = (Text) component.findComponentById(ResourceTable.Id_tv_pay4);
        tvFifth = (Text) component.findComponentById(ResourceTable.Id_tv_pay5);
        tvSixth = (Text) component.findComponentById(ResourceTable.Id_tv_pay6);

        mPassword = new StringBuilder();
        addComponent(component, new LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT));
    }

    /**
     * 输入第一个密码
     *
     * @param first first
     */
    public void setTextFirst(String first) {
        tvFirst.setText(STANCE_STRING);
        mPassword.append(first);
    }

    /**
     * 输入第二个密码
     *
     * @param second second
     */
    public void setTextSecond(String second) {
        tvSecond.setText(STANCE_STRING);
        mPassword.append(second);
    }

    /**
     * 输入第三个密码
     *
     * @param third third
     */
    public void setTextThird(String third) {
        tvThird.setText(STANCE_STRING);
        mPassword.append(third);
    }

    /**
     * 输入第四个密码
     *
     * @param forth forth
     */
    public void setTextForth(String forth) {
        tvForth.setText(STANCE_STRING);
        mPassword.append(forth);
    }

    /**
     * 输入第五个密码
     *
     * @param fifth fifth
     */
    public void setTextFifth(String fifth) {
        tvFifth.setText(STANCE_STRING);
        mPassword.append(fifth);
    }

    /**
     * 输入第六位密码
     *
     * @param sixth sixth
     */
    public void setTextSixth(String sixth) {
        tvSixth.setText(STANCE_STRING);
        mPassword.append(sixth);
    }

    /**
     * 输入密码
     *
     * @param value value
     */
    public void add(String value) {
        if (mPassword != null && mPassword.length() < 6) {
            mPassword.append(value);
            if (mPassword.length() == 1) {
                tvFirst.setText(STANCE_STRING);
            } else if (mPassword.length() == 2) {
                tvSecond.setText(STANCE_STRING);
            } else if (mPassword.length() == 3) {
                tvThird.setText(STANCE_STRING);
            } else if (mPassword.length() == 4) {
                tvForth.setText(STANCE_STRING);
            } else if (mPassword.length() == 5) {
                tvFifth.setText(STANCE_STRING);
            } else if (mPassword.length() == 6) {
                tvSixth.setText(STANCE_STRING);
                inputEnd();
            }
        }
    }

    /**
     * 删除密码
     */
    public void remove() {
        if (mPassword != null && mPassword.length() > 0) {
            if (mPassword.length() == 1) {
                tvFirst.setText("");
            } else if (mPassword.length() == 2) {
                tvSecond.setText("");
            } else if (mPassword.length() == 3) {
                tvThird.setText("");
            } else if (mPassword.length() == 4) {
                tvForth.setText("");
            } else if (mPassword.length() == 5) {
                tvFifth.setText("");
            } else if (mPassword.length() == 6) {
                tvSixth.setText("");
            }
            mPassword.deleteCharAt(mPassword.length() - 1);
        }
    }

    /**
     * 返回输入的内容
     *
     * @return 返回输入内容
     */
    public String getText() {
        return (mPassword == null) ? null : mPassword.toString();
    }

    /**
     * 当密码输入完成时的回调接口
     */
    public interface OnInputFinishedListener {
        void onInputFinished(String password);
    }

    /**
     * 对外开放的方法
     *
     * @param onInputFinishedListener onInputFinishedListener
     */
    public void setOnInputFinishedListener(OnInputFinishedListener onInputFinishedListener) {
        this.onInputFinishedListener = onInputFinishedListener;
    }

    private void inputEnd() {
        if (isEmpty(tvFirst.getText()) || isEmpty(tvSecond.getText()) || isEmpty(tvThird.getText()) || isEmpty(tvForth.getText()) || isEmpty(tvFifth.getText()) || isEmpty(tvSixth.getText())) {
            return;
        }
        if (onInputFinishedListener != null && mPassword != null && mPassword.toString().length() == 6) {
            onInputFinishedListener.onInputFinished(mPassword.toString());
        }
    }


    private boolean isEmpty(CharSequence str) {
        if (str == null || str.length() == 0) {
            return true;
        } else {
            return false;
        }

    }
}
